package org.algorithm.biubiu.wheel_1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;

/**
 * @author jiazhaoxin
 * @Date 2022/2/15
 * @Desc {https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci/}
 */
public class 第K个节点 {
    public int kthToLast(ListNode head, int k) {
        if(head==null){
            return -1;
        }
        ListNode left = head;
        ListNode right  = head;
        for(int i = 0 ;i<k;i++){
            right = right.next;
        }
        while(right!=null){
            left = left.next;
            right = right.next;
        }
        return left.val;
    }
}
